<?php

namespace common\logics;

use Yii;
use common\helpers\ArrayHelper;

/**
 * 推荐内容
 * 
 * @author emhome <emhome@163.com>
 * @since 1.0
 */
class PositionLogic extends \common\models\position\Position {

    public static function getModel($name) {
        $id = is_numeric($name) ? $name : ArrayHelper::getValue(Yii::$app->params, ['position', $name]);
        if ($id === null) {
            return static::findOne([
                'label' => $name
            ]);
        }
        return $id ? static::findOne($id) : null;
    }

    /**
     * 获取当前导航 
     * @return array
     */
    public static function current($name, $default = []) {
        $model = static::getModel($name);
        if (!$model) {
            return $default;
        }
        $list = $model->list;
        if (empty($list)) {
            return $default;
        }
        if ($model->limits > 1) {
            return $list;
        }
        return $list[0];
    }

    /**
     * 获取当前导航 
     * @return array
     */
    public static function findByName($name, $default = []) {
        $model = static::findOne(['label' => $name]);
        if (!$model) {
            return $default;
        }
        $list = $model->list;
        if (empty($list)) {
            return $default;
        }
        if ($model->limits > 1) {
            return $list;
        }
        return $list[0];
    }

}
